package io.github.uicdb.badskill;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.capabilities.CapabilityToken;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerSkillCapabilityProvider implements ICapabilitySerializable<CompoundTag> {
    private final LazyOptional<PlayerSkillCapability> lazyOptional;
    public static final Capability<PlayerSkillCapability> CAPABILITY= CapabilityManager.get(new CapabilityToken<>() {
    });
    public PlayerSkillCapabilityProvider(Player player) {
        this.lazyOptional = LazyOptional.of(()->new PlayerSkillCapability(player));
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        if(cap.equals(CAPABILITY))
            return lazyOptional.cast();
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        return lazyOptional.resolve().orElseThrow().serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        lazyOptional.resolve().orElseThrow().deserializeNBT(nbt);
    }
}
